﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Controls
{
    public partial class PCombobox : ComboBox
    {
        public PCombobox()
        {
            InitializeComponent();
            //this.DrawMode = DrawMode.OwnerDrawFixed;
            //this.DrawItem += Draw;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
        }

        private void Draw(object sender, DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            System.Drawing.Rectangle rect = e.Bounds;
            //定义要绘制到控件中的图标图像
            ////Image ico = Image.FromFile("head.png");
            //定义字体对象
            System.Drawing.Font font = new System.Drawing.Font(new FontFamily("宋体"), 12);
            if (e.Index >= 0)
            {
                //获得当前Item的文本
                string tempString = this.Items[e.Index].ToString();
                //如果当前项是没有状态的普通项
                if (e.State == DrawItemState.None)
                {
                    //在当前项图形表面上划一个矩形
                    g.FillRectangle(new SolidBrush(Color.FromArgb(200, 230, 255)), rect);
                    //在当前项图形表面上划上图标
                    ////g.DrawImage(ico, new Point(rect.Left, rect.Top));
                    //在当前项图形表面上划上当前Item的文本
                    g.DrawString(tempString, font, new SolidBrush(Color.Black), rect.Left /*+ ico.ScreenSize.ScreenWidth*/, rect.Top);
                    //将绘制聚焦框
                    e.DrawFocusRectangle();
                }
                else
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), rect);
                    ////g.DrawImage(ico, new Point(rect.Left, rect.Top));
                    g.DrawString(tempString, font, new SolidBrush(Color.Black), rect.Left /*+ ico.ScreenSize.ScreenWidth*/, rect.Top);
                    e.DrawFocusRectangle();
                }
            }
        }
    }
}
